package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

//编写aopAdvice类
public class AOPAdvice {
    public void before(JoinPoint joinPoint) {
        System.out.println("参数数组：" + joinPoint.getArgs());
        System.out.println("目标对象：" + joinPoint.getTarget());
        System.out.println("方法签名："+joinPoint.getSignature());
        System.out.println("方法名：" + joinPoint.getSignature().getName());
        System.out.println("我在方法前执行");
    }
    public void after(){
        System.out.println("我在方法后执行");
    }
    public void afterReturning(){
        System.out.println("我在方法后执行");
    }

    public void afterThrowing(Exception e) {
        System.out.println("异常的信息"+e.getMessage());
        System.out.println("我在方法抛出异常后执行");
    }

    /**
     * 环绕增强的实现方式
     * @return
     */
    public Object around(ProceedingJoinPoint joinPoint) {
            try {
                System.out.println("前置增强");
                //环绕增强必须调用原始方法
                //方法的返回值
                Object result = null;
                result = joinPoint.proceed(joinPoint.getArgs());
                System.out.println("后置增强");
                return result ;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("异常增强");
            } finally {
            System.out.println("最终增强");
        }
        return null;
    }
}
